package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import wiremock.com.github.jknack.handlebars.Lambda;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;
    @Autowired
    private SetMealDishService setMealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

//    套餐的分页查询，类似菜品，都有一个多的数据需要添加，需要用到setMealDto
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public Result<Page> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> PageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setMealDtoPage =new Page<>();
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(PageInfo,lqw);

        BeanUtils.copyProperties(PageInfo,setMealDtoPage,"records");

        List<Setmeal> records = PageInfo.getRecords();
        List<SetmealDto> list=new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Category category = categoryService.getById(record.getCategoryId());
//            有的套餐可能并没有设置category,就查不出来category
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
                list.add(setmealDto);
            }
        }
        setMealDtoPage.setRecords(list);
        return Result.success(setMealDtoPage);
    }
//    套餐信息的保存，包括对setMeal和SetMealDish两张表的保存
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    @CacheEvict(value = "setMealCache",allEntries = true)
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        setMealService.saveWithDish(setmealDto);
        return Result.success("保存成功");
    }
//    套餐信息表的获取，在套餐进行修改时用于回显数据
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询套餐接口")
    public Result<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto=setMealService.getWithDish(id);
        return Result.success(setmealDto);
    }
//    修改套餐信息，包括对SetMeal和SetMealDish两张表的修改
    @PutMapping
    @ApiOperation(value = "套餐更新接口")
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        setMealService.updateWithDish(setmealDto);
        return Result.success("修改成功");
    }
//    改变套餐的售卖状态
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态接口")
    public Result<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            setmeal.setStatus(status);
            setMealService.updateById(setmeal);
        }
        return Result.success("修改成功");
    }
//    删除套餐及对应的setMealDish的数据
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    @CacheEvict(value = "setMealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        setMealService.deleteWithDish(ids);
        return Result.success("删除成功");
    }
//    返回套餐分类对应下的具体套餐数据
    @GetMapping("/list")
    @ApiOperation("套餐条件查询接口")
    @Cacheable(value = "setMealCache",key = "#setmeal.categoryId+'_1'")
    public Result<List<Setmeal>> SelectByCategoryId(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setMealService.list(lqw);
        return Result.success(list);
    }

//    移动端点击套餐返回套餐下的具体菜品数据
//    因为还要展示份数，要用DishDto
    @GetMapping("/dish/{id}")
    public Result<List<DishDto>> dish(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        lqw.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setMealDishService.list(lqw);
        List<DishDto> list1=new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            DishDto dishDto=new DishDto();
            dishDto.setCopies(setmealDish.getCopies());
            LambdaQueryWrapper<Dish> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(Dish::getId,setmealDish.getDishId());
            Dish one = dishService.getOne(lqw1);
            BeanUtils.copyProperties(one,dishDto);
            list1.add(dishDto);
        }
        return Result.success(list1);
    }
}
